<?php
namespace app\admin\controller;
use app\admin\model\IndexTableModel;
use app\admin\model\JoinTableModel;
use think\Db;
use think\Request;

require_once (__DIR__.'/../../../vendor/ReidsLock.class.php');
class FalableController extends Controller
{

    /**
     * 标签列表
     * User:@hang
     */
    public function index(Request $request)
    {
        $param = $request->param();
        $whehe = array();
        $whehe[] = ['fid','=',"0"];
        if (isset($param['label_name']) && !empty($param['label_name'])) {
            $whehe[] = ['label_name','like',"%{$param['label_name']}%"];
        }
        if (isset($param['status']) && !empty($param['status'])) {
            $whehe[] = ['status','=',$param['status']];
        }
        $data = Db::table('fa_label')
            ->where($whehe)
            ->field('*')
            ->order('sort asc,add_time asc')
            ->paginate($this->admin['per_page'],false,['query'=>request()->param()]);
        $data->each(function ($v, $k) {
            switch($v['status']){
                case '1': $v['status_word'] = '显示';break;
                case '2': $v['status_word'] = '隐藏';break;
            }
            $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
            return $v;
        });
        //关键词，排序等赋值
        $this->assign($request->get());
        $this->assign([
            'data'  => $data,
            'page'  => $data->render(),
            'total' => $data->total(),
        ]);
        return $this->fetch();
    }

    /**
     * 添加父级标签
     * User:@hang
     */
    public function addflable(Request $request)
    {
        if ($request->isPost()) {
            $param = $request->param();
            //操作上锁
            $Redis_Lock = new \Redislock();
            $otc_sell_key = 'lock:addflable';    // 定义锁标识
            $is_lock = $Redis_Lock->lock($otc_sell_key, 15);    // 获取锁
            //解锁失败
            if (!$is_lock) {
                return admin_error('操作频繁，请稍后再试！');
            }
            if (empty($param['label_name']) || !isset($param['label_name'])) {
                $Redis_Lock->unlock($otc_sell_key);    //释放锁
                return admin_error('请输入父级标签名称');
            }
            if (empty($param['status']) || !isset($param['status'])) {
                $Redis_Lock->unlock($otc_sell_key);    //释放锁
                return admin_error('请选择标签状态');
            }
            $IndexTableModel = New IndexTableModel();
            $info = $IndexTableModel->getFind('fa_label', "`label_name` = '{$param['label_name']}'",'id');
            if(!empty($info)){
                $Redis_Lock->unlock($otc_sell_key);	//释放锁
                return admin_error('您添加的标签已经存在，请检查');
            }

            $datas = array();
            $datas['fid'] = 0;
            $datas['label_name'] = $param['label_name'];
            $datas['status'] = $param['status'];
            $datas['sort'] = $param['sort'];
            $datas['add_time'] = time();
            $res = $IndexTableModel->addInfo('fa_label', $datas);
            if(empty($res)){
                $Redis_Lock->unlock($otc_sell_key);	//释放锁
                return admin_error('添加父级标签失败');
            }
            $Redis_Lock->unlock($otc_sell_key);	//释放锁
            return admin_success('添加父级标签成功','url://back');
        }
        return $this->fetch('addflable');
    }

    /**
     * 编辑父级标签
     * User:@hang
     */
    public function editflable($id,Request $request)
    {
        $IndexTableModel = New IndexTableModel();
        $data = $IndexTableModel->getFind('fa_label',array('id'=>$id),'*');
        if ($request->isPost()) {
            $param = $request->param();
            //操作上锁
            $Redis_Lock = new \Redislock();
            $otc_sell_key = 'lock:editflable';    // 定义锁标识
            $is_lock = $Redis_Lock->lock($otc_sell_key, 15);    // 获取锁
            //解锁失败
            if (!$is_lock) {
                return admin_error('操作频繁，请稍后再试！');
            }
            if (empty($param['label_name']) || !isset($param['label_name'])) {
                $Redis_Lock->unlock($otc_sell_key);    //释放锁
                return admin_error('请输入父级标签名称');
            }
            if (empty($param['status']) || !isset($param['status'])) {
                $Redis_Lock->unlock($otc_sell_key);    //释放锁
                return admin_error('请选择标签状态');
            }
            $info = $IndexTableModel->getFind('fa_label', "`label_name` = '{$param['label_name']}' and `id` != '{$id}'",'id');
            if(!empty($info)){
                $Redis_Lock->unlock($otc_sell_key);	//释放锁
                return admin_error('标签名称已经存在，请检查');
            }
            $datas = array();
            $datas['fid'] = 0;
            $datas['label_name'] = $param['label_name'];
            $datas['status'] = $param['status'];
            $datas['sort'] = $param['sort'];
            $res = $IndexTableModel->editInfo('fa_label',array('id'=>$id), $datas);
            if(empty($res)){
                $Redis_Lock->unlock($otc_sell_key);	//释放锁
                return admin_error('编辑父级标签失败');
            }
            $Redis_Lock->unlock($otc_sell_key);	//释放锁
            return admin_success('编辑父级标签成功','url://back');
        }
        $this->assign([
            'data' => $data,
        ]);
        return $this->fetch('addflable');
    }


    /**
     * 管理子标签
     * User:@hang
     */
    public function showzlable(Request $request)
    {
        $id = input('id');
        $param = $request->param();
        $whehe = array();
        $whehe[] = ['fid','=',$id];
        $IndexTableModel = New IndexTableModel();
        $finfo = $IndexTableModel->getFind('fa_label', "`id` = '$id'",'id,label_name');
        if(empty($finfo)){
            return admin_error('父级标签信息查询失败');
        }
        if (isset($param['label_name']) && !empty($param['label_name'])) {
            $whehe[] = ['label_name','like',"%{$param['label_name']}%"];
        }
        if (isset($param['status']) && !empty($param['status'])) {
            $whehe[] = ['status','=',$param['status']];
        }
        $data = Db::table('fa_label')
            ->where($whehe)
            ->field('*')
            ->order('sort asc,add_time asc')
            ->paginate($this->admin['per_page'],false,['query'=>request()->param()]);
        $data->each(function ($v, $k) {
            switch($v['status']){
                case '1': $v['status_word'] = '显示';break;
                case '2': $v['status_word'] = '隐藏';break;
            }
            $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
            return $v;
        });
        //关键词，排序等赋值
        $this->assign($request->get());
        $this->assign([
            'data'  => $data,
            'page'  => $data->render(),
            'total' => $data->total(),
            'fid' => $id,
            'fname' => $finfo['label_name'],
        ]);
        return $this->fetch();
    }

    /**
     * 添加子标签
     * User:@hang
     */
    public function addzlable(Request $request)
    {
        $IndexTableModel = New IndexTableModel();
        $fid = input('fid');
        $finfo = $IndexTableModel->getFind('fa_label', "`id` = '$fid'",'id,label_name');
        if(empty($finfo)){
            return admin_error('父级标签信息查询失败');
        }
        if ($request->isPost()) {
            $param = $request->param();
            //操作上锁
            $Redis_Lock = new \Redislock();
            $otc_sell_key = 'lock:addzlable';    // 定义锁标识
            $is_lock = $Redis_Lock->lock($otc_sell_key, 15);    // 获取锁
            //解锁失败
            if (!$is_lock) {
                return admin_error('操作频繁，请稍后再试！');
            }
            if (empty($param['label_name']) || !isset($param['label_name'])) {
                $Redis_Lock->unlock($otc_sell_key);    //释放锁
                return admin_error('请输入子标签名称');
            }
            if (empty($param['status']) || !isset($param['status'])) {
                $Redis_Lock->unlock($otc_sell_key);    //释放锁
                return admin_error('请选择标签状态');
            }
            $IndexTableModel = New IndexTableModel();
            $info = $IndexTableModel->getFind('fa_label', "`label_name` = '{$param['label_name']}'",'id');
            if(!empty($info)){
                $Redis_Lock->unlock($otc_sell_key);	//释放锁
                return admin_error('您添加的标签已经存在，请检查');
            }

            $datas = array();
            $datas['fid'] = $param['fid'];
            $datas['label_name'] = $param['label_name'];
            $datas['status'] = $param['status'];
            $datas['sort'] = $param['sort'];
            $datas['add_time'] = time();
            $res = $IndexTableModel->addInfo('fa_label', $datas);
            if(empty($res)){
                $Redis_Lock->unlock($otc_sell_key);	//释放锁
                return admin_error('添加子标签失败');
            }
            $Redis_Lock->unlock($otc_sell_key);	//释放锁
            return admin_success('添加子标签成功','url://back');
        }
        $this->assign([
            'finfo' => $finfo,
            'fid' => $fid,
        ]);
        return $this->fetch('addzlable');
    }

    /**
     * 编辑子标签
     * User:@hang
     */
    public function editzlable($id,Request $request)
    {
        $IndexTableModel = New IndexTableModel();
        $data = $IndexTableModel->getFind('fa_label',array('id'=>$id),'*');
        $finfo = $IndexTableModel->getFind('fa_label', "`id` = {$data['fid']}",'id,label_name');
        if(empty($finfo)){
            return admin_error('父级标签信息查询失败');
        }
        if ($request->isPost()) {
            $param = $request->param();
            //操作上锁
            $Redis_Lock = new \Redislock();
            $otc_sell_key = 'lock:editzlable';    // 定义锁标识
            $is_lock = $Redis_Lock->lock($otc_sell_key, 15);    // 获取锁
            //解锁失败
            if (!$is_lock) {
                return admin_error('操作频繁，请稍后再试！');
            }
            if (empty($param['label_name']) || !isset($param['label_name'])) {
                $Redis_Lock->unlock($otc_sell_key);    //释放锁
                return admin_error('请输入子标签名称');
            }
            if (empty($param['status']) || !isset($param['status'])) {
                $Redis_Lock->unlock($otc_sell_key);    //释放锁
                return admin_error('请选择标签状态');
            }
            $info = $IndexTableModel->getFind('fa_label', "`label_name` = '{$param['label_name']}' and `id` != '{$id}'",'id');
            if(!empty($info)){
                $Redis_Lock->unlock($otc_sell_key);	//释放锁
                return admin_error('标签名称已经存在，请检查');
            }
            $datas = array();
            $datas['label_name'] = $param['label_name'];
            $datas['status'] = $param['status'];
            $datas['sort'] = $param['sort'];
            $res = $IndexTableModel->editInfo('fa_label',array('id'=>$id), $datas);
            if(empty($res)){
                $Redis_Lock->unlock($otc_sell_key);	//释放锁
                return admin_error('编辑子标签失败');
            }
            $Redis_Lock->unlock($otc_sell_key);	//释放锁
            return admin_success('编辑子标签成功','url://back');
        }
        $this->assign([
            'data' => $data,
            'finfo' => $finfo,
        ]);
        return $this->fetch('addzlable');
    }



}
